﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class EventEditor : Form
    {
        Event evt;
        public EventEditor(Event e)
        {
            this.evt = e;
            InitializeComponent();
        }

        void UpdateDisplay()
        {
            edit_eventname.Text = evt.name;
            listBox_actions.BeginUpdate();
            listBox_actions.Items.Clear();
            foreach(Action act in evt.actions)
            {
                listBox_actions.Items.Add(string.Format("{0}->{1}: {2}", act.emitter, act.receiver, act.description));
            }
            listBox_actions.EndUpdate();
        }

        private void EventEditor_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        private void btn_createAction_Click(object sender, EventArgs e)
        {
            evt.actions.Add(new Action());
            UpdateDisplay();
        }

        private void btn_deleteAction_Click(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            if (idx < 0) return;
            if(MessageBox.Show("确定要删除这个动作吗？", "删除动作", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                evt.actions.RemoveAt(idx);
                UpdateDisplay();
            }
        }

        private void edit_eventname_TextChanged(object sender, EventArgs e)
        {
            evt.name = edit_eventname.Text;
        }

        private void listBox_actions_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            if (idx < 0) return;
            ActionEditor ae = new ActionEditor(evt.actions[idx]);
            ae.ShowDialog();
            UpdateDisplay();
        }

        private void EventEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.mainform.UpdateDisplay();
        }
    }
}
